let api = require('./api.js');
class EasyHttp {
  post(url, data = {}) {
    data.token = wx.getStorageSync('token')
    return new Promise((resolve, reject) => {
      wx.request({
        url,
        data: data,
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        success(res) {
          if (res.data.status == -100 || res.data.status == -101) {
            EasyHttp.loginByWeixin().then(prop => {
              data.token = wx.getStorageSync('token')
              if (prop.data.status == 1) {
                wx.request({
                  url,
                  data: data,
                  method: 'POST',
                  header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                  },
                  success(result) {
                    resolve(result)
                  },
                  fail(err) {
                    reject(reject)
                  }
                })
              }
            }).catch(res => {
              resolve(res)
            })
          } else {
            if (res.statusCode == 500) {
              wx.hideLoading();
              wx.showModal({
                content: '接口请求出错，请重新操作',
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
                success(e) {}
              })
              reject('接口请求出错,请重新操作')
            } else {
              resolve(res)
            }
          }
        },
        fail(err) {
          wx.hideLoading();
          wx.showModal({
            content: '网络连接失败,请检查网络状况',
            showCancel: false,
            confirmText: '我知道了',
            confirmColor: '#ff5151',
            success(e) {}
          })
          reject('网络连接失败,请检查网络状况')
        }
      })
    })
  }
  get(url, data = {}) {
    data.token = wx.getStorageSync('token')
    return new Promise((resolve, reject) => {
      wx.request({
        url,
        data: data,
        method: 'GET',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(reject)
        }
      })
    })
  }
  static loginByWeixin() {
    return new Promise(function (resolve, reject) {
      wx.login({
        success: function (res) {
          if (res.code) {
            wx.request({
              url: api.reqUrls.miniLogin,
              data: {
                code: res.code
              },
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded',
              },
              success(res) {
                if (res.data.status == 1) {
                  wx.setStorageSync('token', res.data.result.token)
                  getApp().globalData.token = res.data.result.token
                  resolve(res)
                }
              },
              fail(err) {
                reject(reject)
              }
            })
          }
        },
        fail: function (err) {
          console.log(err)
        }
      });
    });

  }
}
module.exports = {
  EasyHttp: new EasyHttp
}